package org.aom.litebean.module.menu;

import org.aom.entity.menu.Menu;
import org.aom.litebean.module.base.BaseGridCrudBean;
import org.aom.service.menu.IMenuManager;
import org.aom.util.ManageBeanUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;

@ManagedBean(name = "module.menu.menuListBean", scope = ManagedBeanScope.REQUEST)
public class MenuListBean extends BaseGridCrudBean<Menu, Long> {

	// ----------关于页面表格------//
	@ManagedProperty("#{menuManager}")
	private IMenuManager menuManager;

	@Bind(id = "menuDataGrid", attribute = "binding")
	private UIDataGrid menuDataGrid;

	@Bind(id = "menuDataGrid", attribute = "idColumn")
	private static String idColumn = "id";

	@Bind(id = "menuDataGrid", attribute = "dataProvider")
	public GridDataProvider getDataProvider() {
		return super.getDataProvider();
	}

	@Override
	protected Class<Menu> getEntityClass() {
		return Menu.class;
	}

	@Override
	protected IMenuManager getEntityManager() {
		return this.menuManager;
	}

	@Override
	protected String getIdColumn() {
		return idColumn;
	}

	// ------关于实体的细节操作------//

	@Override
	protected void initAdd() {
	}

	@Override
	@Action(id = "add", event = "onclick")
	public void processToAdd() {
		String url = "menuForm.faces";
		ManageBeanUtil.openDialog(null, "add", OperationFrame, url,
				OperationDialog, 400, 150);
	}

	@Override
	@Action(id = "menuDataGrid", event = "ondblclick")
	public void processToEdit() {
		try {
			Menu menu = (Menu) getSelectEntity();
			String url = "menuForm.faces";
			ManageBeanUtil.openDialog(menu.getId(), "mod", OperationFrame, url,
					OperationDialog, 400, 150);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Action(id = "linkModuleItems")
	public void linkModuleItem() {
		try {
			Menu menu = (Menu) getSelectEntity();
			String url = "userLinkMenu.faces";
			ManageBeanUtil.openDialog(menu.getId(), null, OperationFrame, url,
					OperationDialog, 800, 400);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Override
	protected void processToSearch() {

	}

	// ----初始化资源---//
	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		initBaseResource();
	}

	@Override
	protected UIDataGrid getDataGrid() {
		return this.menuDataGrid;
	}

	@Override
	protected void delete() {
		try {
			Menu entity = (Menu) getSelectEntity();
			menuManager.delete(entity.getId());
			resetGrid();
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}
}
